﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for JLTVKWZ.xaml
    /// </summary>
    public partial class JLTVKWZ : DocumentBase
    {
        #region-------------Properties-----------------------
        string Mandant = "00"; 
        bool IsSetUp = false;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        string PrintType = "";
   
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTVKWZData m_Data, m_DataX;
        DataRow dwTemp;
        BackgroundWorker bgWorker;
        string lang
        {
            get 
            {
                return txtLang.Text.Trim();
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.TryFindResource("SetData");
            }
        }
        CollectionViewSource vwWBBST
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwWBBST");
            }
        }
        DataRow dwWBBST
        {
            get
            {
                if (vwWBBST.View == null)
                    return null;
                if (vwWBBST.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWBBST.View.CurrentItem).Row;
            }
        }
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        
        public DataSet dsParamaters;
        JLTVKWZReport m_rpt;
        ArrayList listVV06, ListColor, listBatchNo;
        string KRS
        {
            get
            {
                return Helper.Left(txtVKPAUFKRS.Text.ToString() + "  ", 2);
            }
        }
        string LFD
        {
            get
            {
                return Helper.Right("000000" + txtVKPAUFLFD.Text.ToString(), 6);
            }
        }
        string POS
        {
            get
            {
                return Helper.Right("000" + txtVKPAUFPOS.Text.ToString(), 3);
            }
        }
        #endregion
        public JLTVKWZ()
        {
            InitializeComponent();
            this.Name = "布卷清单";
        } 
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
               app.Load(); 

                }
               
                Mandant = (string)dicParamater["MANDANT"];

                m_Data = new JLTVKWZData(); 
                #region============TESTINGDATA================
                txtVKPAUFKRS.Text = "J" + m_Data.date.Substring(3, 1); 
                
                #endregion
                bgWorker = new BackgroundWorker();
                bgWorker.DoWork += bgWorker_DoWork;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
           
        }
        private void GenerateReport(string PrintType, string ReportType="ALLOCATED")
        {
            try
            {
                JLTVKWZReport m_rpt = new JLTVKWZReport();
                JLTVKWZData m_DX = new JLTVKWZData();
                m_rpt.dsSource = SetData;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DX.exAgent; 
                m_rpt.ReportType = ReportType;
                m_rpt.WriteExcelReport(PrintType, lang);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
 
        private void btnPrintWB_Click(object sender, RoutedEventArgs e)
        {
            PrintType = "OpenExcel";
            GenerateReport(PrintType,"FREE");
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bool byArt = false;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SetData.Clear();
                    SetData.AcceptChanges();
                    dwTemp = SetData.VKPOS.NewRow();
                    dwTemp["VKPMANDANT"] = Mandant;
                    dwTemp["VKPAUFKRS"] = KRS;
                    dwTemp["VKPAUFLFD"] = LFD;
                    dwTemp["VKPAUFPOS"] = POS;
                    if (ChkCP.IsChecked == true)
                    {
                        byArt = true;
                        dwTemp["VKPART"] = Helper.Left(KRS + "-" + Helper.Right(LFD, 4) + "                                ", 20);
                        if (POS == "000")
                        {
                            dwTemp["VKPFARBE"] = "            ";
                        }
                        else
                        {
                            dwTemp["VKPFARBE"] = Helper.Left(Convert.ToInt16(POS) + "                  ", 10);
                        }
                        dwTemp["VKPAENDST"] = txtLB.Text;
                    }
                    SetData.Merge(m_Data.BuildData(dwTemp, byArt));
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void cmdUpdateWBBPACKNR_Click(object sender, RoutedEventArgs e)
        {
            if (dwWBBST == null)
                return;
            dwWBBST["WBBMANDANT"] = "00";
            INTEXDBC.UpdateWBBST(dwWBBST, 2);
            vwWBBST.View.MoveCurrentToNext();
        }

        private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                cmdUpdateWBBPACKNR_Click(null, null);
            }
        }
    }
}
